package com.zpark.se.io.raf;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * @PackageName: com.zpark.se.io.raf
 * @ClassName: Demo03
 * @Description:
 * @author: RZS
 * @date: 2024/11/18  10:56
 */
public class Demo03 {
    public static void main(String[] args) {
        // 声明路径
        String path = "src/com/zpark/se/io/raf/content/raf.txt";
        RandomAccessFile raf = null;
        try {
            // 创建随机文件读写对象
            raf = new RandomAccessFile(path, "rw");

            // 向文件当中写数据

            /*
             * 写入指定的字节
             *
             */
            raf.write(10);

            // 定义需要写入的数据
            String str = "hello world";
            // 将字符串转为字节数组
            byte[] bs = str.getBytes("utf-8");

            /*
             * 写入指定的字节数组
             */
            raf.write(bs);

            /*
             * 写入指定的boolean值
             */
            raf.writeBoolean(true);

            raf.writeBytes("床前明月光");



        } catch(FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (raf != null) {
                try {
                    // 关闭文件对象
                    raf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
